﻿var io = require('socket.io-client');
var robotInfo = require('./robot').getInstand;
var log = require("./../../util/loginfo").getInstand;
ttzUser = function (_userInfo) {
    //相关属性
    this._userId = 0;
    this.TotalCount = Math.floor(Math.random() * 1200) + 60;
    this.timer;

    this.isDown = false;
    this.isZhuang = false;
    this.upPro = 20;     //上庄概率
    this.downPro = 5;   //下庄概率

    //离线后再次登录时间
    this._online = false;
    this._out = false;

    this.jieguo = [0, 0, 0];
    this.win = 0;
    this.point = -1;

    this.init = function (_userInfo) {
        this.setAccount(_userInfo.account);
        this._socre = 100;
        this.idx = _userInfo.idx;
        this.connect();
        this.time();
    };

    this.setAccount = function (_Account) {
        this._account = _Account;
    };

    this.getAccount = function () {
        return this._account;
    };

    this.EnterLoginServer = function () {
        this._socket.emit('login', {
            userName: this.getAccount(),
            sign: '@@@@youmegame2019`@112233445566@@@@',
            gameId: 1
        })
    };


    this.connect = function () {
        this._socket = io.connect('http://localhost:13000');
        //this._socket = io.connect('http://192.168.1.170:3000');
        this.initNetWork();
        this.EnterLoginServer();
    };

    //连接游戏服务器
    this.connectGame = function () {
        this._gameSocket = io.connect('http://' + this._ip + ':' + this._port);
        ////log.info(this._ip)
        this.initGameSocket();
        //进入游戏,被服务器分配房间
        this.logingame();
    };

    //进入游戏
    this.logingame = function () {
        if (this._gameSocket && this._userId) {
            this._gameSocket.emit('LoginGame', {userid: this._userId, sign: this._sign})
        }
    };

    //进入房间
    this.loginroom = function () {
        this._gameSocket.emit('LoginRoom', {roomid: 1});
    };

    //接收大厅信息
    this.initNetWork = function () {
        var self = this;
        this._socket.on('connected', function (msg) {
            ////log.info(msg);
        });

        this._socket.on('loginResult', function (result) {
            ////log.info(result)
            if (result.resultid !== 1) {
                self.disconnect();
                return;
            }
            self._sign = result.Obj.sign;
            self._userId = result.Obj.id;
            self._socre = result.Obj.score;

        });

        this._socket.on('ServerListResult', function (msg) {
            log.info(self._userId + "进入推筒子");
            self._ip = "localhost";
            self._port = "16011";

            //进入游戏
            self.connectGame();
        })

    };

    this.down = function () {
        if (this._out) {
            return;
        }
        --this.TotalCount;
        let i = RandomNumBoth(0, 100);
        if (this._online) {
            if (this.isZhuang) {
                if (i < this.downPro) {
                    this.downZ();
                }
            } else {
                if (i < this.upPro) {
                    this.upZ();
                }
            }
        }
    };

    this.time = function () {
        if (!this.timer) {
            var second = 1000;
            this.timer = setInterval(this.down.bind(this), second);
        }
    };

    this.out = function () {
        this.disconnect();
        clearInterval(this.timer);
        this._out = true;
    };


    //接收游戏信息
    this.initGameSocket = function () {
        var self = this;
        this._gameSocket.on('connected', function (msg) {
            ////log.info(msg);
        });

        this._gameSocket.on('loginGameResult', function (msg) {
            ////log.info(msg);
            //结束与登录服务器通信
            self._online = true;
            self.loginroom();
        });
        //开始下注
        this._gameSocket.on('downCoinBegin', function (msg) {
            self.sendCardCointimer = setTimeout(self.lottery.bind(self), Math.floor((Math.random() * 9) * 1000) + 1000);
        });
        //下注结束
        this._gameSocket.on('downEnd', function (msg) {

        });
        //开奖
        this._gameSocket.on('openResult', function (msg) {

        });
        //下注结果返回
        this._gameSocket.on('downCoinResult', function (msg) {

        });
        //其他玩家下注
        this._gameSocket.on('otherDownCoin', function (msg) {

        });
        //上庄结果返回
        this._gameSocket.on('upResult', function (msg) {
            if (msg.ResultCode) {
                self.isZhuang = true;
            }
        });
        //下庄结果返回
        this._gameSocket.on('downResult', function (msg) {
            if (1 === msg.ResultCode) {
                self.isZhuang = false;
            }
        });
        //他人上庄结果
        this._gameSocket.on('otherUp', function (msg) {

        });
        //他人下庄结果
        this._gameSocket.on('otherDown', function (msg) {

        });
        //中奖通知
        this._gameSocket.on('winResult', function (msg) {

        });

        this._gameSocket.on('getDownTimeResult', function (msg) {

        });
    };

    this.lottery = function () {
        clearInterval(this.sendCardCointimer);
        ////log.info(this._userId);
        if (this._online) {
            try {
                this._gameSocket.emit('downCoin', JSON.stringify({
                    chips: RandomNumBoth(0, 4),
                    selectId: RandomNumBoth(0, 2)
                }));
            } catch (e) {

            }
        }

    };

    this.upZ = function () {
        this._gameSocket.emit('up', JSON.stringify({
            Coin: RandomNumBoth(300, 600) * 100
        }));
    };

    this.downZ = function () {
        this._gameSocket.emit('down');
    };

    this.disconnect = function () {
        if (this._gameSocket) {
            this._gameSocket.disconnect();
            this._gameSocket = null;
        }
        if (this._socket) {
            this._socket.disconnect();
            this._socket = null;
        }
        this._online = false;
        this.isDown = true;
        //log.info(this._userId + "离开游戏")
        clearInterval(this.timer);
    };

    this.isOnline = function () {
        return this.TotalCount > 0 || this.isDown;
    };

    this.init(_userInfo);

};


function RandomNumBoth(Min, Max) {
    //生成指定范围内随机整数
    var Range = Max - Min;
    var Rand = Math.random();
    var num = Min + Math.round(Rand * Range); //四舍五入
    return num;
}

function RandomNumForList(arr) {
    //从指定数组中选取随机值
    return arr[Math.floor((Math.random() * arr.length))]
}

module.exports = ttzUser;